using System;
using UnityEngine;
using Voodoo.Sauce.Common.Utils;

namespace Voodoo.Sauce.Internal.Utils
{
	public static class DeviceUtils
	{
		private const string TAG = "DeviceUtils";

		private static string _iosModelMachineName;

		private static string _operatingSystemVersion;

		private static string _manufacturer;

		private static string _model;

		private const string BuildClassName = "android.os.Build";

		private static readonly string _versionClassName = "android.os.Build$VERSION";

		private const string ConnectivityOffline = "Offline";

		private const string ConnectivityNetwork = "Network";

		private const string ConnectivityWifi = "Wifi";

		private const string ConnectivityUnknown = "Unknown";

		private static string _lastConnectivity = "Unknown";

		private static string _installStore;

		public static string OperatingSystemVersion => _operatingSystemVersion ?? (_operatingSystemVersion = GetOperatingSystemVersion());

		public static string Manufacturer => _manufacturer ?? (_manufacturer = GetManufacturer());

		public static string Model => _model ?? (_model = GetModel());

		public static bool HasBottomSafeArea()
		{
			return (float)Screen.height > Screen.safeArea.y + Screen.safeArea.height;
		}

		public static bool HasNotch()
		{
			if (!(Screen.safeArea.width < (float)Screen.width))
			{
				return Screen.safeArea.height < (float)Screen.height;
			}
			return true;
		}

		public static string GetResolution()
		{
			int num = ((Screen.height > Screen.width) ? Screen.height : Screen.width);
			int num2 = ((Screen.height > Screen.width) ? Screen.width : Screen.height);
			return num + "x" + num2;
		}

		private static T CallDeviceInformationMethod<T>(string className, string method)
		{
			using AndroidJavaClass androidJavaClass = new AndroidJavaClass(className);
			return androidJavaClass.GetStatic<T>(method);
		}

		public static string GetManufacturer()
		{
			if (PlatformUtils.UNITY_EDITOR)
			{
				return "Unknown(Editor)";
			}
			if (PlatformUtils.UNITY_IOS)
			{
				return "Apple";
			}
			if (PlatformUtils.UNITY_ANDROID)
			{
				return CallDeviceInformationMethod<string>("android.os.Build", "MANUFACTURER");
			}
			return "Unknown()";
		}

		public static string GetModel()
		{
			if (PlatformUtils.UNITY_IOS && !PlatformUtils.UNITY_EDITOR)
			{
				return UnityIosDevice.Generation;
			}
			if (PlatformUtils.UNITY_ANDROID && !PlatformUtils.UNITY_EDITOR)
			{
				return CallDeviceInformationMethod<string>("android.os.Build", "MODEL");
			}
			return SystemInfo.deviceModel;
		}

		public static string GetOperatingSystemVersion()
		{
			if (PlatformUtils.UNITY_IOS && !PlatformUtils.UNITY_EDITOR)
			{
				return UnityIosDevice.SystemVersion;
			}
			if (PlatformUtils.UNITY_ANDROID && !PlatformUtils.UNITY_EDITOR)
			{
				return string.Format("Android API {0}", CallDeviceInformationMethod<int>(_versionClassName, "SDK_INT"));
			}
			return SystemInfo.operatingSystem;
		}

		internal static string GetConnectivity()
		{
			if (UnityThreadExecutor.IsMainThread)
			{
				switch (Application.internetReachability)
				{
				case NetworkReachability.NotReachable:
					_lastConnectivity = "Offline";
					break;
				case NetworkReachability.ReachableViaCarrierDataNetwork:
					_lastConnectivity = "Network";
					break;
				case NetworkReachability.ReachableViaLocalAreaNetwork:
					_lastConnectivity = "Wifi";
					break;
				default:
					_lastConnectivity = "Unknown";
					break;
				}
				return _lastConnectivity;
			}
			if (_lastConnectivity == "Unknown")
			{
				VoodooLog.LogError(Module.COMMON, "DeviceUtils", "Impossible to get the device connectivity right now.");
			}
			return _lastConnectivity;
		}

		private static string _getNativeLocale()
		{
			return "";
		}

		internal static string GetAndroidInstallStore()
		{
			if (!PlatformUtils.UNITY_ANDROID || PlatformUtils.UNITY_EDITOR)
			{
				return null;
			}
			if (_installStore == null)
			{
				using AndroidJavaClass androidJavaClass = new AndroidJavaClass("io.voodoo.installstore.InstallStore");
				_installStore = androidJavaClass.CallStatic<string>("Get", new object[2] { "not_found", "no_source" });
			}
			return _installStore;
		}

		internal static string GetLocale()
		{
			if (PlatformUtils.UNITY_IOS && !PlatformUtils.UNITY_EDITOR)
			{
				return _getNativeLocale();
			}
			if (PlatformUtils.UNITY_ANDROID && !PlatformUtils.UNITY_EDITOR)
			{
				using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("java.util.Locale"))
				{
					using AndroidJavaObject androidJavaObject = androidJavaClass.CallStatic<AndroidJavaObject>("getDefault", Array.Empty<object>());
					return androidJavaObject.Call<string>("getCountry", Array.Empty<object>());
				}
			}
			return "us";
		}
	}
}
